{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 分析有无正常数据与故障数据挨得比较近的",
   "id": "8998f7e26932cfd1"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-23T02:55:15.650071Z",
     "start_time": "2024-10-23T02:55:15.645072Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "from datetime import datetime, timedelta\n",
    "import math"
   ],
   "id": "3ff061ffdad3683e",
   "outputs": [],
   "execution_count": 62
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-23T02:55:15.666055Z",
     "start_time": "2024-10-23T02:55:15.651067Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data_num = 15\n",
    "dir = f'../data/raw/train'\n",
    "data_file = f'{dir}/{data_num}/{data_num}_data.csv'\n",
    "data_normal_file = f'{dir}/{data_num}/{data_num}_normalInfo.csv'\n",
    "data_failure_file = f'{dir}/{data_num}/{data_num}_failureInfo.csv'\n"
   ],
   "id": "984b3b84b3efa423",
   "outputs": [],
   "execution_count": 63
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-23T02:55:15.682008Z",
     "start_time": "2024-10-23T02:55:15.667010Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df_normal = pd.read_csv(data_normal_file)\n",
    "df_normal[\"startTime\"] = pd.to_datetime(df_normal[\"startTime\"])\n",
    "df_normal[\"endTime\"] = pd.to_datetime(df_normal[\"endTime\"])\n",
    "normal_time_list = [(row[\"startTime\"], row[\"endTime\"]) for _, row in df_normal.iterrows()]\n",
    "\n",
    "df_failure = pd.read_csv(data_failure_file)\n",
    "df_failure[\"startTime\"] = pd.to_datetime(df_failure[\"startTime\"])\n",
    "df_failure[\"endTime\"] = pd.to_datetime(df_failure[\"endTime\"])\n",
    "failure_time_list = [(row[\"startTime\"], row[\"endTime\"]) for  _, row in df_failure.iterrows()]"
   ],
   "id": "5154c4d4c4afc1c0",
   "outputs": [],
   "execution_count": 64
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-23T02:55:15.696866Z",
     "start_time": "2024-10-23T02:55:15.683009Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# print(normal_time_list)\n",
    "# print(failure_time_list)"
   ],
   "id": "d055d6c2e918f41b",
   "outputs": [],
   "execution_count": 65
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 组内判断时间区间小于xxx的相邻时间区间",
   "id": "5d79704d64d8ab9b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-23T02:55:15.712618Z",
     "start_time": "2024-10-23T02:55:15.697868Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "def check_near_time_in_group(time_list):\n",
    "    for i in range(1, len(time_list)):\n",
    "        end1 = time_list[i-1][1]\n",
    "        stat2 = time_list[i][0]\n",
    "        if (stat2 - end1).total_seconds() < 60 * 60 * 6:\n",
    "            print(f'end1: {end1}, stat2: {stat2}, 间隔小')\n",
    "        if (end1 - stat2).total_seconds() < 60 * 60:\n",
    "            pass\n",
    "        \n",
    "check_near_time_in_group(normal_time_list)\n",
    "print(\"___________________________\")\n",
    "check_near_time_in_group(failure_time_list)"
   ],
   "id": "31bd25ba6454c0df",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "___________________________\n",
      "end1: 2015-11-25 22:48:07, stat2: 2015-11-26 01:47:51, 间隔小\n"
     ]
    }
   ],
   "execution_count": 66
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 两组之间判断相隔小于xxx的时间区间",
   "id": "84882dd2487d1b1e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-23T02:55:15.728619Z",
     "start_time": "2024-10-23T02:55:15.713619Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "def check_near_intervals(group1, group2, threshold=1):\n",
    "    for start1, end1 in group1:\n",
    "        for start2, end2 in group2:\n",
    "            # 检查结束时间与开始时间之间的间隔\n",
    "            \n",
    "            if 0 < (start2 - end1).total_seconds() < threshold * 60 * 60 *12:\n",
    "                print(f\"Group 1 interval ({end1} to {start1}) is near Group 2 interval ({start2})\")\n",
    "                return True\n",
    "            if 0 < (start1 - end2).total_seconds() < threshold * 60 * 60 *12:\n",
    "                print(f\"Group 2 interval ({end2} to {start2}) is near Group 1 interval ({start1})\")\n",
    "                return True\n",
    "    return False\n",
    "\n",
    "# 检查\n",
    "has_near_intervals = check_near_intervals(normal_time_list, failure_time_list)\n",
    "print(\"有相隔小于xxx的时间区间:\", has_near_intervals)"
   ],
   "id": "aabb53a10c724e14",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Group 1 interval (2015-11-04 19:42:35 to 2015-11-01 17:33:56) is near Group 2 interval (2015-11-04 22:15:38)\n",
      "有相隔小于xxx的时间区间: True\n"
     ]
    }
   ],
   "execution_count": 67
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-23T02:55:15.743619Z",
     "start_time": "2024-10-23T02:55:15.729619Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "3985ec5f7110f514",
   "outputs": [],
   "execution_count": 67
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
